/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.main.controller;

import com.kimstore.bean.Item;
import com.kimstore.bean.ItemStatus;
import com.kimstore.bean.RepairDetail;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.main.Inventory;
import com.kimstore.gui.repair.AddItemForRepairController;
import com.kimstore.gui.repair.ViewItemForRepairController;
import com.kimstore.utilities.CustomTableRowSorter;
import com.kimstore.utilities.DateUtil;
import com.kimstore.utilities.PaginateSearchAndFilter;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class RepairTabController extends BaseTabController {

    private Inventory parentController;
    private PaginateSearchAndFilter<Item> psaf;

    public RepairTabController(Inventory parentController) {
        this.parentController = parentController;
    }

    public void refreshTable() {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
        populateRepairTable();
    }

    public void populateRepairTable() {
        populateRepairTable(psaf.search());
    }

    private void populateRepairTable(List<Item> items) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
        if (items != null) {
            for (Item i : items) {
                model.insertRow(model.getRowCount(), new Object[]{i.getSerial(),
                            i.getProduct().getName(),
                            formatDate(i.getRepairDetail().getReceiveDate()),
                            formatDate(i.getRepairDetail().getReleaseDate()),
                            i.getRepairDetail().getCurrentStatus(),
                            i.getLatestStatus()});
            }
        }
    }

    @Override
    public void prepareUserInterface() {
        //searchField.setPrompt(searchFieldPromptText);
        table.setShowGrid(true, true);
        table.addHighlighter(HighlighterFactory.createSimpleStriping());
    }

    @Override
    public void bindElements() {
        table.setRowSorter(new CustomTableRowSorter(table.getRowSorter().getModel()) {
            @Override
            public void sort() {
                if (table.isShowing()) {
                    Point p = MouseInfo.getPointerInfo().getLocation();
                    int column = getSelectedColumn(p);
                    if (psaf.isColumnSortable(column)) {
                        populateRepairTable(psaf.sort(column));
                    }
                }
            }
        });
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                populateRepairTable(psaf.search());
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Item> items = psaf.nextPage();
                if (items != null) {
                    populateRepairTable(items);
                }
            }
        });

        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Item> items = psaf.previousPage();
                if (items != null) {
                    populateRepairTable(items);
                }
            }
        });
    }

    private String formatDate(Date date) {
        if (date != null) {
            return DateUtil.formatDate(date);
        }
        return "-";
    }

    public void createAddItemToRepairController() {
        AddItemForRepairController controller = new AddItemForRepairController(this);
        controller.displayWindow();
    }

    public void createAddItemToRepairControllerCallback(Item item) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.insertRow(model.getRowCount(), new Object[]{item.getSerial(),
                    item.getProduct().getName(),
                    DateUtil.formatDate(item.getRepairDetail().getReceiveDate()),
                    "-",
                    item.getRepairDetail().getCurrentStatus(),
                    item.getLatestStatus()});
    }

    public void createViewItemForRepairController(String SKU) {
        ViewItemForRepairController controller = new ViewItemForRepairController(this, SKU);
        controller.displayWindow();
    }

    public PaginateSearchAndFilter<Item> getPsaf() {
        return psaf;
    }

    public void setPsaf(PaginateSearchAndFilter<Item> psaf) {
        this.psaf = psaf;
    }

    private int getSelectedColumn(Point point) {
        int mousex = point.x - table.getLocationOnScreen().x;
        int mousey = point.y - table.getLocationOnScreen().y;
        Point mousep = new Point(mousex, mousey);

        return table.columnAtPoint(mousep);
    }
}
